<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Watch extends MY_Controller {
    
    function Watch()
    {
        parent::__construct();
        $this->load->model('song_mod');
        $this->_checktoken();
    }
    
    public function by_toprated()
    {
        $result = array();
        $orderby = 'rate';
        
        $datasong  = $this->song_mod->get_songs($orderby.' desc');
        if($datasong) {
            $result['result'] = TRUE;
            $result['songs'] = $datasong;
        } else {
            $result['result'] = FALSE;
        }
        $this->_outputjson($result);
    }
    
    public function by_title()
    {
        $result = array();
        $orderby = 'title';
        
        $datasong  = $this->song_mod->get_songs($orderby.' asc');
        if($datasong) {
            $result['result'] = TRUE;
            $result['songs'] = $datasong;
        } else {
            $result['result'] = FALSE;
        }
        $this->_outputjson($result);
    }
    
    public function by_country()
    {
        $result = array();
        $orderby = 'country';
        
        $datasong  = $this->song_mod->get_songs($orderby.' asc');
        if($datasong) {
            $result['result'] = TRUE;
            $result['songs'] = $datasong;
        } else {
            $result['result'] = FALSE;
        }
        $this->_outputjson($result);
    }
    
    public function by_country_genre()
    {
        $result = array();
        $orderby = 'country';
        $orderby2 = 'genre';
        
        $datasong  = $this->song_mod->get_songs($orderby.' asc, '.$orderby2.' asc');
        if($datasong) {
            $result['result'] = TRUE;
            $result['songs'] = $datasong;
        } else {
            $result['result'] = FALSE;
        }
        $this->_outputjson($result);
    }
    
    public function search()
    {
        $result = array();
        $keyword = $this->input->post('keyword');
        $field = $this->input->post('field');
        if(empty($field)) {$field = 'title';}
        
        $datasong  = $this->song_mod->search_song($keyword, $field);
        if($datasong) {
            $result['result'] = TRUE;
            $result['songs'] = $datasong;
        } else {
            $result['result'] = FALSE;
        }
        $this->_outputjson($result);
    }
    
    public function video()
    {
        $result = array();
        $idsong = $this->input->post('idsong');
        $username = $this->session->userdata('username');
        if(!empty($idsong) && !empty($username)) {
            $dataplay  = $this->song_mod->get_song($idsong,'video');
            if($dataplay) {
                //Set URL
                //$url = $this->config->item('server_rtsp').$dataplay['path_file'];
                //$dataplay['url'] = $url;
 $rtsp = $this->song_mod->_getRTSP($idsong);
                $dataplay['rtsp_video'] = $rtsp['video'];
                $dataplay['rtsp_audio'] = $rtsp['audio'];                
				$result['result'] = TRUE;
                $result['song'] = $dataplay;
                //Insert Action Log
                $this->song_mod->add_log('watch', $idsong, $username);
            } else {
                $result['result'] = FALSE;
            }
            $this->_outputjson($result);
        }
    }
    
    public function audio()
    {
        $result = array();
        $idsong = $this->input->post('idsong');
        $username = $this->session->userdata('username');
        if(!empty($idsong) && !empty($username)) {
            $dataplay  = $this->song_mod->get_song($idsong,'audio');
            if($dataplay) {
                //Set URL
                //$url = $this->config->item('server_rtsp').$dataplay['path_file'];
                //$dataplay['url'] = $url;
				$rtsp = $this->song_mod->_getRTSP($idsong);
                $dataplay['rtsp_video'] = $rtsp['video'];
                $dataplay['rtsp_audio'] = $rtsp['audio'];
                $result['result'] = TRUE;
                $result['song'] = $dataplay;
                //Insert Action Log
                $this->song_mod->add_log('watch', $idsong, $username);
            } else {
                $result['result'] = FALSE;
            }
            $this->_outputjson($result);
        }
    }
    
    public function video_record()
    {
        $result = array();
        $idrec = $this->input->post('idrecord');
        if(!empty($idrec)) {
            $dataplay  = $this->song_mod->get_record($idrec,'video');
            if($dataplay) {
                //Set URL
                //$url = $this->config->item('server_rtsp').$dataplay['path_file'];
                //$dataplay['url'] = $url;
 $rtsp = $this->song_mod->_getRTSP($idsong);
                $dataplay['rtsp_video'] = $rtsp['video'];
                $dataplay['rtsp_audio'] = $rtsp['audio'];                
$result['result'] = TRUE;
                $result['record'] = $dataplay;
            } else {
                $result['result'] = FALSE;
            }
            $this->_outputjson($result);
        }
    }
    
    public function audio_record()
    {
        $result = array();
        $idrec = $this->input->post('idrecord');
        if(!empty($idrec)) {
            $dataplay  = $this->song_mod->get_record($idrec,'audio');
            if($dataplay) {
                //Set URL
                //$url = $this->config->item('server_rtsp').$dataplay['path_file'];
                //$dataplay['url'] = $url;
$rtsp = $this->song_mod->_getRTSP($idsong);
                $dataplay['rtsp_video'] = $rtsp['video'];
                $dataplay['rtsp_audio'] = $rtsp['audio'];                
$result['result'] = TRUE;
                $result['result'] = TRUE;
                $result['record'] = $dataplay;
            } else {
                $result['result'] = FALSE;
            }
            $this->_outputjson($result);
        }
    }
    
    public function rate_song()
    {
        $result = array();
        $username = $this->session->userdata('username');
        $idsong = $this->input->post('idsong');
        $rate = $this->input->post('rate');
        if(!empty($username) && !empty($idsong) && !empty($rate)) {
            $data = array(
                "id_song" => $idsong,
                "username" => $username,
                "rate" => $rate,
                "insert_date" => date('Y-m-d H:i:s')
            );
            //cek jika user sudah memasukan rating sebelumnya
            $hasrate = $this->song_mod->check_user_rate($username, $idsong);
            if($hasrate) {
                //Update rate dari user
                $this->song_mod->update_rate($data, $hasrate['id']);
                $result['result'] = TRUE;
            } else {
                //Insert new rate
                $this->song_mod->add_rate($data);
                $result['result'] = TRUE;
            }
            //Insert Action Log
            $this->song_mod->add_log('rate', $idsong, $username);
        } else {
            $result['result'] = FALSE;
        }
        
        $this->_outputjson($result);
    }
    
    public function rate_record()
    {
        $result = array();
        $username = $this->session->userdata('username');
        $idrec = $this->input->post('idrecord');
        $rate = $this->input->post('rate');
        if(!empty($username) && !empty($idrec) && !empty($rate)) {
            $data = array(
                "id_record" => $idrec,
                "username" => $username,
                "rate" => $rate,
                "insert_date" => date('Y-m-d H:i:s')
            );
            //cek jika user sudah memasukan rating sebelumnya
            $hasrate = $this->song_mod->check_user_rate_record($username, $idsong);
            if($hasrate) {
                //Update rate dari user
                $this->song_mod->update_rate_record($data, $hasrate['id']);
                $result['result'] = TRUE;
            } else {
                //Insert new rate
                $this->song_mod->add_rate_record($data);
                $result ['result'] = TRUE;
            }
        } else {
            $result ['result'] = FALSE;
        }
        
        $this->_outputjson($result);
    }
}